<?php
/**
 * MySQLi DB wrapper
 * - handles all SQL transactions, called by the class models
 * - returns last inserted id on INSERT
 * 
 * @author Andreas Psaros 2010-09-06
 */

class Database {
    protected $_mysqli;

    /* Database settings defined in config */
    private $dbhost = DBHOST;
    private $dbuser = DBUSER;
    private $dbpass = DBPASS;
    private $dbname = DBNAME;

    /* settings */
    private $_debug = DEBUG;
    private $dbcharset = DBCHARSET;

    function __construct(){
        $this->_mysqli = new mysqli($this->dbhost, $this->dbuser, $this->dbpass, $this->dbname);

        // handle errors
        if (mysqli_connect_errno()) {
            if ($this->_debug) {
                echo mysqli_connect_error();
                debug_print_backtrace();
            }
            return false;
        }

        // Set charset
        mysqli_set_charset($this->_mysqli, $this->dbcharset);

        return true;
    }

    public function query($query) {

        $toReturn = false;

        if ($query = $this->_mysqli->prepare($query)) {
            if (func_num_args() > 1) {
                $x = func_get_args();
                $args = array_merge(array(func_get_arg(1)),
                    array_slice($x, 2));
                $args_ref = array();
                foreach($args as $k => &$arg) {
                    $args_ref[$k] = &$arg;
                }
                call_user_func_array(array($query, 'bind_param'), $args_ref);
            }
            $query->execute();

            if ($query->errno) {
              if ($this->_debug) {
                echo mysqli_error($this->_mysqli);
                debug_print_backtrace();
              }
              //return false;
              $toReturn = false;
            }

            // INSERT: return last inserted id
            if ($query->affected_rows > -1) {
                //return $query->affected_rows;
                $toReturn = $this->_mysqli->insert_id;
            }
            else {
                $params = array();
                $meta = $query->result_metadata();
                while ($field = $meta->fetch_field()) {
                    $params[] = &$row[$field->name];
                }
                call_user_func_array(array($query, 'bind_result'), $params);

                $result = array();
                while ($query->fetch()) {
                    $r = array();
                    foreach ($row as $key => $val) {
                        $r[$key] = $val;
                    }
                    $result[] = $r;
                }
                $query->close();
                //return $result;
                $toReturn = $result;
            }
        } else {
            if ($this->_debug) {
                echo $this->_mysqli->error;
                debug_print_backtrace();
            }
            //return false;
            $toReturn = false;
        }

        // stripslashes
        if($toReturn){
            $toReturn = stripslashes_deep($toReturn);
        }
        
        return $toReturn;
    }

    public function handle() {
        return $this->_mysqli;
    }

}

?>
